<?php

namespace App\Admin\Controllers;

use App\Models\Car;

use App\Models\MyCar;
use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Facades\Admin;
use Encore\Admin\Layout\Content;
use App\Http\Controllers\Controller;
use Encore\Admin\Controllers\ModelForm;

class CarController extends Controller
{
    use ModelForm;

    /**
     * Index interface.
     *
     * @return Content
     */
    public function index()
    {
        return Admin::content(function (Content $content) {

            $content->header('车辆管理');

            $content->body($this->grid());
        });
    }

    /**
     * Edit interface.
     *
     * @param $id
     * @return Content
     */
    public function edit($id)
    {
        return Admin::content(function (Content $content) use ($id) {

            $content->header('编辑车辆');

            $content->body($this->form()->edit($id));
        });
    }

    /**
     * Create interface.
     *
     * @return Content
     */
    public function create()
    {
        return Admin::content(function (Content $content) {

            $content->header('创建车辆');

            $content->body($this->form());
        });
    }

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Admin::grid(Car::class, function (Grid $grid) {

            $grid->model()->orderByDesc('forSell')->orderByDesc('CarID');
            $grid->CarID('ID')->sortable();
            
            $grid->CarImgBig('图片')->image('',50,50);
            $grid->CarName('名称');
            
            //$grid->CarColor('颜色');
            $grid->CarType('类型')->display(function($color){
                return config('car.cfgCarType')[$color];
            });
            
            $grid->CarPrice('价格(金币)');
            $grid->CarNum('库存');
            $grid->CarCredit('每分钟停车积分');
            
            $grid->column('saled','已售')->display(function(){
               return MyCar::where('CarID',$this->CarID)->count()?:0;
            });
            //$grid->column('second_hand','二手车')->display(function(){
            //    return MyCar::withoutGlobalScopes()->where('isSale',1)->where('CarID',$this->CarID)->count()?:0;
            //});
            
            $grid->forSell('是否上架销售')->switch();
        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Admin::form(Car::class, function (Form $form) {

            $form->display('CarID', 'ID');
            
            $form->text('CarName','名称')->rules('required');
            $form->text('CarDesc','简介')->rules('required');
            $form->text('link','车型介绍外链');
            
            $form->image('CarSign','小图标')->uniqueName();
            $form->image('CarImg','商城图')->uniqueName();
            $form->image('CarImgBig','展位图')->uniqueName();
            //$form->image('CarSign','标志');
            
            //$form->radio('CarColor','颜色')->options(config('car.cfgColor'));
            $form->radio('CarType','类型')->options(config('car.cfgCarType'))->default(1);
            
            $form->number('CarPrice','价格');
            $form->number('CarNum','库存');
            
            $form->number('CarCredit','停车每分钟积分');
            $form->switch('forSell','是否上架销售');

        });
    }
    
    public function destroy($id)
    {
        //删除前的判断
        //if(MyCar::where('CarID',$id)->exists()){
        //    return response()->json([
        //        'status'  => false,
        //        'message' => '删除失败，还有相关记录未删除',
        //    ]);
        //}
        if ($this->form()->destroy($id)) {
            MyCar::where('CarID',$id)->delete();
            return response()->json([
                'status'  => true,
                'message' => trans('admin.delete_succeeded').'已删除相关记录',
            ]);
        } else {
            return response()->json([
                'status'  => false,
                'message' => trans('admin.delete_failed'),
            ]);
        }
    }
}
